/*--------------------------------------------------------------*
 * Creat Date: 2015/7/20 										*
 * Modify    : 
 *                                     
 *--------------------------------------------------------------*/
 #include "at24xx.h"
 
/*****************************************************************************//*!
   *
   * @brief initialize parameter.
   *        
   * @param[in] DataAdd - Data address in the chip
   * @param[in] Len     - length
   * @param[in] Rsc     - point to Data 
   * @return none
   *
   * @ Pass/ Fail criteria: none
*****************************************************************************/ 
uint8_t at24Write(uint32_t DataAdd,uint16_t Len,uint8_t  *Rsc)
{
	uint8_t Sla;
	uint16_t Sda;
	uint16_t LenCur;
	uint16_t AddrInPage;
	uint8_t SdaType;
	uint32_t DataAddrCur;
	
	AddrInPage  = DataAdd & EEP_PAGE_MASK;
	while(Len)
	{
		LenCur = EEP_PAGE_SIZE - AddrInPage;
		if( LenCur < Len)        
		{
			DataAddrCur = DataAdd;
			
#if (EEP_ADDR_TYPE & 0x01) == 0	
			SdaType = 1;                       //one byte sda
			Sda  = DataAddrCur & 0xFF;
			DataAddrCur     >>= 8; 
#else
			SdaType = 2;                       //Two byte sda
			Sda  = DataAddrCur & 0xFFFF;
			DataAddrCur     >>= 16; 
#endif

#if ((EEP_ADDR_TYPE & EEP_INTER_SLA_MASK) == 0)
			Sla = EEP_CHIP_ADDR;
#else
			Sla = EEP_CHIP_ADDR | ((EEP_ADDR_TYPE & EEP_INTER_SLA_MASK) & (DataAddrCur << 1));
#endif						
			while(I2C1_MasterSendBytes(Sla,Sda,SdaType,Rsc,LenCur) != I2C_ERROR_NULL);
			DataAdd   += LenCur;
			Rsc       += LenCur;
			Len       -= LenCur;
			AddrInPage = 0;
		}
		else                                     
		{
#if (EEP_ADDR_TYPE & 0x01) == 0	
			SdaType = 1;                       //one byte sda
			Sda  = DataAdd & 0xFF;
			DataAdd     >>= 8; 
#else
			SdaType = 2;                      //Two byte sda
			Sda  = DataAdd & 0xFFFF;
			DataAdd     >>= 16; 
#endif

#if ((EEP_ADDR_TYPE & EEP_INTER_SLA_MASK) == 0)
			Sla = EEP_CHIP_ADDR;
#else
			Sla = EEP_CHIP_ADDR | ((EEP_ADDR_TYPE & EEP_INTER_SLA_MASK) & (DataAdd << 1));
#endif						
			while(I2C1_MasterSendBytes(Sla,Sda,SdaType,Rsc,Len) != I2C_ERROR_NULL);
			
			Len = 0;
		}
	}	
	
	return I2C_ERROR_NULL;
}


/*****************************************************************************//*!
   *
   * @brief initialize parameter.
   *        
   * @param[in] DataAdd - Data address in the chip
   * @param[in] Len     - length
   * @param[in] Des     - point to destination
   * @return none
   *
   * @ Pass/ Fail criteria: none
*****************************************************************************/
uint8_t at24Read(uint32_t DataAdd,uint16_t Len,uint8_t *Des)
{
	uint8_t Sla;
	uint16_t Sda;
	uint8_t SdaType;
	
#if (EEP_ADDR_TYPE & 0x01) == 0	
	SdaType = 1;                       //one byte sda
	Sda  = DataAdd & 0xFF;
	DataAdd     >>= 8; 
#else
	SdaType = 2;                      //Two byte sda
	Sda  = DataAdd & 0xFFFF;
	DataAdd     >>= 16; 
#endif

#if ((EEP_ADDR_TYPE & EEP_INTER_SLA_MASK) == 0)
	Sla = EEP_CHIP_ADDR;
#else
	Sla = EEP_CHIP_ADDR | ((EEP_ADDR_TYPE & EEP_INTER_SLA_MASK) & (DataAdd << 1));
#endif
	
	while(I2C1_MasterReadBytes(Sla,Sda,SdaType,Des,Len) != I2C_ERROR_NULL);
	return I2C_ERROR_NULL;
} 

